<!-- 
    @file: 首页精选模块组件
    @auth: lizibin
 -->
<template>
    <div class="featured" v-loading="loading" element-loading-text="玩命加载中...">
        <div class="featured_title">
            <h3 class="title"><b>精选</b></h3>
        </div>
        <div class="featured_layout">
            <div v-for="(item, index) in articleInfo">
                <div class="contentBox" v-if="index === 0">
                    <div @click="RouterJump('/articleDetails', item.articleId)" class="articleBox-jump"
                        :title="item.articleTitle">
                        <img v-if="item.articleImgLitimg" class="bannerImg" :src="item.articleImgLitimg"
                            :alt="item.articleTitle">
                        <h4 class="title">{{ item.articleTitle }}</h4>
                        <p>{{ item.articleClassifyName }} · {{ item.publishTime }}</p>
                    </div>
                </div>

                <div class="contentBox item" v-if="index > 0">
                    <div @click="RouterJump('/articleDetails', item.articleId)" class="articleBox-jump"
                        :title="item.articleTitle">
                        <div v-if="item.articleImgLitimg" class="banner">
                            <img :src="item.articleImgLitimg" :alt="item.articleTitle">
                        </div>
                        <!-- 判断是否有缩略图，没有则样式不同  -->
                        <div :class="[item.articleImgLitimg ? 'articleContent' : 'articleContent2']">
                            <h4 class="title">{{ item.articleTitle }}</h4>
                            <p>{{ item.articleClassifyName }} · {{ item.publishTime }}</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">
import {
    onMounted,
    reactive,
    toRefs
} from "vue";

import { SystemSetupHandle, ArticleFiltterBySystem } from '@/utils/utils'
import { useStore } from '@/store/index'
import { storeToRefs } from 'pinia'
import { Reactive } from '@/types/articleTypes'
import {
    useRouter,
    useRoute
} from 'vue-router';
const router = useRouter()
const RouterJump = (name: string, val: number) => {
    router.push({
        path: name,
        query: {
            id: val
        }
    })
}

const Store = useStore()
const { SystemSetup, AllArticleInfo } = storeToRefs(Store)
const state = reactive<Reactive>({
    // 筛选出来的文章内容
    articleInfo: [],
    loading: false
});
const loadData = async (state: Reactive) => {
    state.loading = true
    const { code, data } = SystemSetup.value;
    const { code: ACode, data: AData } = AllArticleInfo.value;
    const featuredData = SystemSetupHandle(data.featuredArticle)

    ArticleFiltterBySystem(featuredData, AData, state.articleInfo)

    state.loading = false
}

const { articleInfo, loading } = toRefs(state)
//挂载后加载数据
onMounted(() => {
    loadData(state);
});

</script>

<style scoped>
.featured {
    background: rgb(255, 255, 255);
    border-radius: 6px;
    padding: 20px;
    margin-bottom: 20px;
}

.articleBox-jump {
    cursor: pointer;
}

.featured_title {
    margin-bottom: 15px;
    margin-top: -10px;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid rgb(238, 238, 238);
    overflow: hidden;
}

.featured_title .title {
    line-height: 40px;
    color: #474749;
}

.featured_title .title b {
    display: inline-block;
    border-bottom: 4px solid #2fa7b9;
}

.featured_layout .contentBox {
    margin-bottom: 15px;
    position: relative;
    overflow: hidden;
    font-size: 12px;
    color: #999;
    line-height: 18px;
}

.featured_layout .contentBox h4:hover {
    color: #2fa7b9;
}

.featured_layout .contentBox span {
    cursor: pointer;
}

.featured_layout .bannerImg {
    display: block;
    width: 100%;
    object-fit: cover;
    margin-bottom: 10px;
    border-radius: 6px;
}

.featured_layout .title {
    font-size: 15px;
    line-height: 24px;
    margin-bottom: 10px;
    color: #454545;
}

.featured_layout p {
    font-size: 12px;
    color: #999;
}

.featured_layout .contentBox.item {
    padding-top: 15px;
    border-top: 1px solid #eee;
}

.featured_layout .contentBox.item .banner {
    width: 30%;
    height: 60px;
    overflow: hidden;
    float: left;
    border-radius: 4px;
    margin-right: 15px;
    cursor: pointer;
}

.featured_layout .contentBox.item .banner img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.featured_layout .contentBox.item .title {
    font-size: 12px;
    line-height: 18px;
    overflow: hidden;
    -webkit-box-orient: vertical;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    text-align: justify;
    cursor: pointer;
    line-height: 20px;
}

.articleContent {
    width: 55%;
    float: left;
    word-break: break-all;
    text-align: justify;
}

.articleContent2 {
    width: 100%;
    float: left;
    word-break: break-all;
    text-align: justify;
}
</style>